﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

namespace Remote_server
{
    /// <summary>
    /// Class used to store settings. It contains a dictionary
    /// and storing and retreiving values is similar to PHP or Ruby
    /// arrays where setting the key for the first time creates an entry
    /// </summary>
    public class Settings
    {
        /// <summary>
        /// The default absolute path to the configuration file
        /// </summary>
        public static readonly string ConfigurationFile;
        /// <summary>
        /// The folder that is the direct parent to the default
        /// configuration file
        /// </summary>
        public const string FilesDirectory = "Remote server";
        /// <summary>
        /// The local name of the default configuration file
        /// </summary>
        public const string DefaultFile = "config.xml";

        private IDictionary<string, string> entries = new Dictionary<string, string>();

        /// <summary>
        /// Static constructor that initializes the
        /// configuration file path
        /// </summary>
        static Settings()
        {
            ConfigurationFile = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                Path.Combine(FilesDirectory, DefaultFile));
        }

        /// <summary>
        /// Initialize the Settings with the default
        /// file
        /// </summary>
        public Settings() : this(ConfigurationFile) { }

        /// <summary>
        /// Initialize the Settings with a specific
        /// file
        /// </summary>
        /// <param name="file">The path to the configuration file</param>
        public Settings(string file)
        {
            try
            {
                XElement xml = XElement.Load(ConfigurationFile);
                foreach (XElement elem in xml.Elements())
                    this[elem.Name.LocalName] = (string)elem;
            }
            catch { }
        }

        /// <summary>
        /// Save settings using the default configuration
        /// file
        /// </summary>
        public void Save() { Save(ConfigurationFile); }

        /// <summary>
        /// Save settings to a specific file
        /// </summary>
        /// <param name="file">The path to the file</param>
        public void Save(string file)
        {
            try
            {
                string dir = new FileInfo(ConfigurationFile).DirectoryName;
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                XElement xml = new XElement("Settings",
                    from e in entries.Keys
                    select new XElement(e, entries[e]));
                xml.Save(file);
            }
            catch { }
        }

        /// <summary>
        /// Retreive or set an entry, a new entry is created
        /// if the key has not been set before
        /// </summary>
        /// <param name="key">The key of the entry</param>
        /// <returns>The queried entry, if the entry was
        /// not found null is returned</returns>
        public string this[string key]
        {
            get
            {
                try { return entries[key]; }
                catch { return null; }
            }
            set
            {
                try { entries.Add(key, value); }
                catch { entries[key] = value; }
            }
        }
    }
}
